﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>有参数的方法</title>
    <meta charset="utf-8" />
    <link href="../../Scripts/easyUI145/themes/icon.css" rel="stylesheet" type="text/css" />
    <link href="../../Scripts/easyUI145/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/easyUI145/jquery.min.js" type="text/javascript"></script>
    <script src="../../Scripts/easyUI145/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../../Scripts/config.js" type="text/javascript"></script>
    <script src="../../Scripts/CommonUnite.js" type="text/javascript"></script>
    <script src="../../Scripts/EasyUIUtility.js" type="text/javascript"></script>
    <script src="../../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../../Comm/Gener.js" type="text/javascript"></script>
    <script src="DoMethodPara.js" type="text/javascript"></script>
    <script type="text/javascript">

        var func = null; //方法实体.
        var attrs = null; //参数的属性.
        var frmData = null;

        $(function () {

            var myPK = GetQueryString("MyPK"); //方法的主键.
            var frmID = GetQueryString("FrmID");
            var workID = GetQueryString("WorkID");
           
            //加载执行方法的js
            loadJs('../../../DataUser/JSLibData/Method/' + myPK + '.js', function () { });

            //初始化方法实体.
            func = new Entity("BP.CCBill.Template.MethodFunc", myPK);
            var methodID = func.MethodID;
            //获得该方法的参数.
            attrs = new Entities("BP.Sys.MapAttrs", "FK_MapData", myPK);

            //初始化表单的数据. JSON.
            frmData = new Entity(func.FrmID, workID);

            document.title = func.MethodName;

            var html = "<table style='width:95%;'>";
            html += "<caption>" + func.MethodName + "</caption>";

            html += InitMapAttr(attrs, frmData);

            html += "</table>";
            html += "<input type='button' value='执行' onclick=\"javascript:DoIt();\" />";
            html += "<input type='button' value='关闭' onclick=\"javascript:closeIt();\" />";

            $("#docs").html(html);
        });
        function loadJs(url,callback){
            var script=document.createElement('script');
            script.type="text/javascript";
            if(typeof(callback)!="undefined"){
                if(script.readyState){
                script.onreadystatechange=function(){
                    if(script.readyState == "loaded" || script.readyState == "complete"){
                    script.onreadystatechange=null;
                    callback();
                    }
                }
                  }else{
                     script.onload=function(){
                      callback();
                 }
            }
         }
            script.src=url;
            document.body.appendChild(script);
        }

        function closeIt() {
            window.close();
        }

        //执行方法.
        function DoIt() {

            if (func.WarningMsg != "") {
                if (confirm(func.WarningMsg) == false)
                    return;
            }

            var handler = new HttpHandler("BP.CCBill.WF_CCBill");
            handler.AddUrlData();
            handler.AddFormData();

            var data = ""; //执行结果.
            //如果是执行的SQL .
            if (func.MethodDocTypeOfFunc == 0) {

                data = handler.DoMethodReturnString("DoMethodPara_ExeSQL");
                if (data.indexOf('err@') >= 0) {
                    $("#Msg").html('系统错误:' + data);
                    return;
                }

            }

            //执行脚本.
            if (func.MethodDocTypeOfFunc == 1) {
                data = ExecScript(func);
            }

             //执行URL.
            if (func.MethodDocTypeOfFunc == 2) {
                data = handler.DoMethodReturnString("DoMethodPara_ExeUrl");
                if (data.indexOf('err@') >= 0) {
                    $("#Msg").html('系统错误:' + data);
                    return;
                }
            }

            //执行的结果. 根据执行成功后的配置，来处理“  执行完毕后干啥？”

            //直接关闭窗口.
            if (func.WhatAreYouTodo == 0) {
                $("#Msg").html(data);
                return;
            }

            //关闭提示窗口并刷新.
            if (func.WhatAreYouTodo == 1) {
                $("#Msg").html(data);
                window.location.href = window.location.href;
                return;
            }

            //转入到Search.htm页面上去.
            if (func.WhatAreYouTodo == 2)
            {
                window.location.href = "SearchBill.htm?FrmID=" + GetQueryString("FrmID");
                return;
            }
        }

        //执行的SCRIPT.
        function ExecScript(func) {

            var scriptFunc = func.DoMethodReturnString("Gener_MethodDoc_JavaScript_function");

            //生成参数.
            var paras = "";
            for (var i = 0; i < attrs.length; i++) {
                var attr = attrs[i];

                var val = "";
                if (attr.UIContralType == 0)
                    val = $("#TB_" + attr.KeyOfEn).val();

                if (attr.UIContralType == 1)
                    val = $("#DDL_" + attr.KeyOfEn).val();

                if (attr.UIContralType == 2)
                    val = $("#CB_" + attr.KeyOfEn).val();

                if (i == attrs.length - 1) {
                    if (attr.MyDataType == 2 || attr.MyDataType == 3 || attr.MyDataType == 4 || attr.MyDataType == 5 || attr.MyDataType == 8) {
                        if (val == "" || val == undefined)
                            val = 0;
                        paras += val;
                    }
                    else
                        paras += "'" + val + "'";
                }
                else {
                    if (attr.MyDataType == 2 || attr.MyDataType == 3 || attr.MyDataType == 4 || attr.MyDataType == 5 || attr.MyDataType == 8) {
                        if (val == "" || val == undefined)
                            val = 0;
                        paras += val + ",";
                    }
                    else
                        paras += "'" + val + "',";
                }
            }

            //执行这个 script.
            try {
                //addDynamicJS(scriptFunc);
                var data = eval(func.MethodID + "(" + paras + ")");
                //func.MethodID + "(" + paras + ")";
                return data;
            } catch (e) {
                alert(e);
                $("#Msg").html(e);
                return;
            }
        }

        function addDynamicJS(src, callback) {
            var script = document.createElement("script");
            script.type = "text/javascript";

            script.charset = 'utf-8';
            try {
                script.appendChild(document.createTextNode(src));
            } catch (ex) {
                script.text = src;
            }
            document.body.appendChild(script);

        }

        //解析表单字段 MapAttr.
        function InitMapAttr(attrs, frmData) {

            var html = "";
            var isDropTR = true;
            for (var i = 0; i < attrs.length; i++) {

                var attr = attrs[i];

                var enable = attr.UIIsEnable == "1" ? "" : " ui-state-disabled";
                var defval = ConvertDefVal(frmData, attr.DefVal, attr.KeyOfEn);

                var lab = "";
                if (attr.UIContralType == 0 || attr.UIContralType == 8)
                    lab = "<label id=Lab_" + attr.KeyOfEn + "'  for='TB_" + attr.KeyOfEn + "' class='" + (attr.UIIsInput == 1 ? "mustInput" : "") + "'>" + attr.Name + "</label>";

                if (attr.UIContralType == 1)
                    lab = "<label id=Lab_" + attr.KeyOfEn + "' for='DDL_" + attr.KeyOfEn + "' class='" + (attr.UIIsInput == 1 ? "mustInput" : "") + "'>" + attr.Name + "</label>";

                if (attr.UIIsInput == 1 && attr.UIIsEnable == 1) {
                    lab += " <span style='color:red' class='mustInput' data-keyofen='" + attr.KeyOfEn + "' >*</span>";
                }

                if (attr.UIContralType == 3)
                    lab = "<label id=Lab_" + attr.KeyOfEn + "' for='RB_" + attr.KeyOfEn + "' class='" + (attr.UIIsInput == 1 ? "mustInput" : "") + "'>" + attr.Name + "</label>";

                //线性展示并且colspan=3
                if (attr.ColSpan == 3 || (attr.ColSpan == 4 && attr.UIHeight < 40)) {
                    isDropTR = true;
                    html += "<tr>";
                    html += "<td  class='FDesc' style='width:120px;'>" + lab + "</td>";
                    html += "<td id='Td_" + attr.KeyOfEn + "' ColSpan=3>";
                    html += InitMapAttrOfCtrl(attr, enable, defval);
                    html += "</td>";
                    html += "</tr>";
                    continue;
                }

                //线性展示并且colspan=4
                if (attr.ColSpan == 4) {
                    isDropTR = true;
                    html += "<tr>";
                    html += "<td id='Td_" + attr.KeyOfEn + "' ColSpan='4'>" + lab + "</br>";
                    html += InitMapAttrOfCtrl(attr, enable, defval);
                    html += "</td>";
                    html += "</tr>";
                    continue;
                }

                if (isDropTR == true) {
                    html += "<tr>";
                    html += "<td class='FDesc' style='width:120px;'>" + lab + "</td>";
                    html += "<td id='Td_" + attr.KeyOfEn + "' class='FContext'  >";
                    html += InitMapAttrOfCtrl(attr, enable, defval);
                    html += "</td>";
                    isDropTR = !isDropTR;
                    continue;
                }

                if (isDropTR == false) {
                    html += "<td class='FDesc' style='width:120px;'>" + lab + "</td>";
                    html += "<td id='Td_" + attr.KeyOfEn + "' class='FContext'>";
                    html += InitMapAttrOfCtrl(attr, enable, defval);
                    html += "</td>";
                    html += "</tr>";
                    isDropTR = !isDropTR;
                    continue;
                }
            }
            if (isDropTR == false) {
                html += "<td class='FDesc' ColSpan='2'></td>";

                html += "</tr>";
            }

            return html;
        }




        //填充默认数据
        function ConvertDefVal(frmData, defVal, keyOfEn) {

            return defVal;


            //计算URL传过来的表单参数@TXB_Title=事件测试
            var result = defVal;

            //            //通过MAINTABLE返回的参数
            //            for (var ele in frmData.MainTable[0]) {
            //                if (keyOfEn == ele && frmData[0] != '') {
            //                    result = frmData[0][ele];
            //                    break;
            //                }
            //            }

            //通过URL参数传过来的参数 后台处理到MainTable 里面
            //for (var pageParam in pageParamObj) {
            //    if (pageParam == keyOfEn) {
            //        result = pageParamObj[pageParam];
            //        break;
            //    }
            //}

            if (result != undefined && typeof (result) == 'string') {
                //result = result.replace(/｛/g, "{").replace(/｝/g, "}").replace(/：/g, ":").replace(/，/g, ",").replace(/【/g, "[").replace(/】/g, "]").replace(/；/g, ";").replace(/~/g, "'").replace(/‘/g, "'").replace(/‘/g, "'");
            }
            return result = unescape(result);
        }

        function DoScript(func) {

        }
    </script>
</head>
<body>
    <form name="cc" id="cc">
        <div id="docs">
        </div>
        <div id="Msg">
        </div>
    </form>
</body>
</html>
